package com.code.google.mdsc.model.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.code.google.mdsc.model.Creditor;

/**
 * 
 */
public class CreditorImpl implements Creditor {

	private String identifier;
	private String country;
	private String iban;
	private String bic;
	private String name;
	private List<String> adress = new ArrayList<String>();

	private static Map<String, String> countryCodeCache = new HashMap<String, String>();

	static {
		String[] countryCodes = Locale.getISOCountries();
		for (int i = 0; i < countryCodes.length; i++) {
			countryCodeCache.put(countryCodes[i], countryCodes[i]);
		}
	}

	public CreditorImpl(String identifier, String country, String iban,
			String bic, String name) {
		super();
		setCountry(country);
		setIdentifier(identifier);
		setIBAN(iban);
		setBIC(bic);
		setName(name);
	}

	@Override
	public void addAdressLine(String adressLine) {
		if (adressLine == null || adressLine.trim().isEmpty())
			throw new IllegalArgumentException(
					"parameter must not be null or an empty string");
		adress.add(adressLine);
	}

	@Override
	public List<String> getAddress() {
		return adress;
	}

	@Override
	public String getBIC() {
		return bic;
	}

	@Override
	public String getCountry() {
		return country;
	}

	@Override
	public String getIBAN() {
		return iban;
	}

	@Override
	public String getIdentifier() {
		return identifier;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setBIC(String bic) {
		if (bic == null || bic.trim().isEmpty())
			throw new IllegalArgumentException(
					"parameter must not be null or an empty string");
		this.bic = bic;
	}

	@Override
	public void setCountry(String country) {
		if (country == null || country.trim().isEmpty())
			throw new IllegalArgumentException(
					"parameter must not be null or an empty string");

		if (!countryCodeCache.containsKey(country.toUpperCase()))
			throw new IllegalArgumentException("value is not iso 3166 conform");
		else
			this.country = countryCodeCache.get(country.toUpperCase());

	}

	@Override
	public void setIBAN(String iban) {
		if (iban == null || iban.trim().isEmpty())
			throw new IllegalArgumentException(
					"parameter must not be null or an empty string");
		this.iban = iban;
	}

	@Override
	public void setIdentifier(String identifier) {
		if (identifier == null || identifier.trim().isEmpty())
			throw new IllegalArgumentException(
					"parameter must not be null or an empty string");
		this.identifier = identifier;
	}

	@Override
	public void setName(String name) {
		if (name == null || name.trim().isEmpty())
			throw new IllegalArgumentException(
					"parameter must not be null or an empty string");
		this.name = name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((adress == null) ? 0 : adress.hashCode());
		result = prime * result + ((bic == null) ? 0 : bic.hashCode());
		result = prime * result + ((country == null) ? 0 : country.hashCode());
		result = prime * result + ((iban == null) ? 0 : iban.hashCode());
		result = prime * result
				+ ((identifier == null) ? 0 : identifier.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CreditorImpl other = (CreditorImpl) obj;
		if (adress == null) {
			if (other.adress != null)
				return false;
		} else if (!adress.equals(other.adress))
			return false;
		if (bic == null) {
			if (other.bic != null)
				return false;
		} else if (!bic.equals(other.bic))
			return false;
		if (country == null) {
			if (other.country != null)
				return false;
		} else if (!country.equals(other.country))
			return false;
		if (iban == null) {
			if (other.iban != null)
				return false;
		} else if (!iban.equals(other.iban))
			return false;
		if (identifier == null) {
			if (other.identifier != null)
				return false;
		} else if (!identifier.equals(other.identifier))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}
